package com.daffodil.workable.factory.strategy;

import java.util.HashMap;
import java.util.Map;

import com.daffodil.util.JacksonUtils;
import com.daffodil.workable.entity.WorkableForm;
import com.daffodil.workable.entity.WorkableNode;
import com.daffodil.workable.entity.WorkableResult;
import com.daffodil.workable.factory.strategy.model.WorkableDataFilter;
import com.daffodil.workable.util.WorkableUtils;

/**
 * 环境变量节点策略
 * @author yweijian
 * @date 2022年4月12日
 * @version 1.0
 * @description
 */
public class DataFilterWorkableStrategy extends DefaultWorkableStrategy{

    @Override
    protected WorkableResult chain(WorkableNode workableNode, WorkableResult curtResult, WorkableResult lastResult) {
        WorkableForm workableForm = workableNode.getWorkableForm();
        WorkableDataFilter workableDataFilter = JacksonUtils.toJavaObject(workableForm, WorkableDataFilter.class);
        Map<String, Object> variables = lastResult.getVariables();
        String env = WorkableUtils.getExpressionValue(workableDataFilter.getFilter(), variables);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("data", JacksonUtils.toJavaObject(env, Map.class));
        curtResult.setVariables(data);
        return curtResult.success();
    }

}
